package cn.tedu.javagame.Plane;

import cn.tedu.javagame.star.Star;
import javafx.scene.layout.Background;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.sql.SQLOutput;
import java.util.*;
import java.util.List;
import java.util.Timer;

public class ShootPanel extends JPanel implements MouseMotionListener {
    //属性
    Image bgImage = new ImageIcon("plane/background.jpg").getImage();//背景图片属性
    int bgY = 0;
    //英雄机属性
    Hero hero = new Hero();
    //存储子弹的集合
    List<Bullet> bullets = new ArrayList<>();//向上造型
    //存储敌机的集合
    List<Enemy> enemys = new ArrayList<>();
    //存储敌机子弹的集合
    List<EnemyBullet> enemyBullets = new ArrayList<>();

    List<FlyObject> flyObjects = new ArrayList<>();
    //构造
    public ShootPanel(){
        //鼠标事件的监听注册
        this.addMouseMotionListener(this);

    }
    //重写父类的paint方法
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        //绘制背景图片
        paintBackground(g);
        //绘制英雄机对象
        paintHero(g);
        //绘制分数，生命值等数据
        paintScore(g);
        //绘制子弹
        paintBullet(g);
        //绘制敌机
//        paintEnemy(g);
        //绘制敌机子弹
        paintFlyObject(g);
        paintEnemyBullet(g);
    }
    Image gg = new ImageIcon("plane/gg.png").getImage();
    //绘制分数，生命值等数据
    public void paintScore(Graphics g){
        g.drawImage(gg, 5,660,null);
        g.setColor(Color.RED);//红色
        //Font的三个参数分别表示字体，加粗，大小
        Font f = new Font("宋体",Font.BOLD,28);
        g.setFont(f);
        g.drawString("得分: "+hero.score,350,710);
        g.drawString("金币: "+hero.gold,230,710 );
        g.drawString("生命: "+hero.life,80,710);
        g.setColor(Color.GREEN);
        g.drawString("关卡： "+(hero.score/1000+1),370,30);
    }
    //绘制背景图片
    public void paintBackground(Graphics g){
        g.drawImage(bgImage,0,bgY,null);
        g.drawImage(bgImage,0,bgY-ShootFrame.HEIGHT,null);
    }
    //绘制英雄机对象
    public void paintHero(Graphics g){
        g.drawImage(hero.heroImg,hero.x,hero.y,null);
    }
    //自定义方法，表示游戏启动
    public void action(){
        //创建定时器对象
        Timer timer = new Timer();
        //启动定时器
        timer.schedule(new TimerTask(){
            public void run(){
                //背景图片的移动
                backgroundMove();
                //英雄机的动态
                hero.step();
                //产生子弹
                bulletAction();
                //移动子弹
                bulletMove();
                //子弹越界的处理
                bulletOutOfBounds();
                //飞行物产生--移动--越界的处理
                flyObjectAction();
                flyObjectMoveAndStep();
                FlyObjectOutOfBounds();
                hitAction();
                //重绘
                repaint();
            }
        },0,10);
    }
    //产生子弹
    int bulletIndex=-1;
    public void bulletAction(){
        bulletIndex++;
        if(bulletIndex==Integer.MAX_VALUE){bulletIndex=-1;}
        if(bulletIndex%20==0){bullets.addAll(hero.shoot());}
    }
    //产生飞行物
    int index = -1;
    public void flyObjectAction(){
        index++;
        if (index == Integer.MAX_VALUE)index = 1;
        if (index%20==0){
            FlyObject flyObject = new Enemy();
            flyObjects.add(flyObject);
        }
        if (index%50==0){
            FlyObject flyObject2 = new Heart();
            flyObjects.add(flyObject2);
        }
    }
    //产生敌机子弹
    int enemyBulletIndex=-1;
    public void enemyBulletAction(){
        enemyBulletIndex++;
        if(enemyBulletIndex==Integer.MAX_VALUE){enemyBulletIndex=-1;}
            if (enemyBulletIndex % 20 == 0) {
//                enemyBullets.addAll(enemys.get(enemyIndex).shoot());
            }


    }
    //绘制子弹
    public void paintBullet(Graphics g){
        for (Bullet bullet : bullets) {
            g.drawImage(Bullet.bulletImg, bullet.x, bullet.y, null);
        }
    }
    //绘制敌机子弹
    public void paintEnemyBullet(Graphics g){
        for (EnemyBullet enemyBullet:enemyBullets){
            g.drawImage(EnemyBullet.bulletImg,enemyBullet.x,enemyBullet.y,null);
        }
    }
    //绘制飞行物
    public void paintFlyObject(Graphics g){
        for (int i=0;i<flyObjects.size();i++){
            FlyObject flyObject = flyObjects.get(i);
//            System.out.println(flyObject.img);
            g.drawImage(flyObject.img,flyObject.x,flyObject.y,null);
        }
    }
    //移动子弹
    public void bulletMove(){
        for (Bullet bullet : bullets) {
            bullet.move();
        }
    }
    //移动敌机子弹
    public void EnemyBulletMove(){
        for (EnemyBullet enemyBullet:enemyBullets) {
            enemyBullet.move();
        }
    }
    //移动飞行物
    public void flyObjectMoveAndStep(){
        for (int i=0;i<flyObjects.size();i++) {
            FlyObject flyObject = flyObjects.get(i);
            flyObject.move();
            flyObject.step();
        }
    }
    //子弹越界的处理
    public void bulletOutOfBounds(){
        for(int i=0;i<bullets.size();i++){
            Bullet bullet = bullets.get(i);
            if(bullet.y<0){bullets.remove(i);}
        }
    }

    //敌机子弹越界的处理
    public void enemyBulletOutOfBounds(){
        for(int i=0;i<enemyBullets.size();i++){
            EnemyBullet enemyBullet = enemyBullets.get(i);
            if(enemyBullet.y<0){enemyBullets.remove(i);}
        }
    }
    //飞行物越界处理
    public void FlyObjectOutOfBounds(){
        for(int i=0;i<flyObjects.size();i++){
            FlyObject flyObject = flyObjects.get(i);
            if(flyObject.y>ShootFrame.HEIGHT){flyObjects.remove(i);}
        }
    }
    //碰撞逻辑
    public void hitAction(){
        for (int i =0;i < flyObjects.size();i++){
            FlyObject flyObject = flyObjects.get(i);
            if (flyObject instanceof Heart){
                Heart heart = (Heart)flyObject;
                if (hero.hitHeart(heart))flyObjects.remove(heart);
            }
            if (flyObject instanceof Enemy){
                Enemy enemy = (Enemy)flyObject;
                if (hero.hitEnemy(enemy)){
                    flyObjects.remove(enemy);
                }
                for (int j = 0;j<bullets.size();j++){
                    Bullet bullet = bullets.get(j);
                    if (bullet.hitEnemy(enemy)){
                        flyObjects.remove(enemy);
                        hero.score += 10;
                    }
                }
            }
        }
    }
    //背景图片的移动
    public void backgroundMove(){
        bgY++;
        if(bgY>=ShootFrame.HEIGHT){bgY=0;}
    }


    @Override
    public void mouseDragged(MouseEvent e) {

    }

    @Override
    public void mouseMoved(MouseEvent e) {
        hero.move(e.getX()-hero.heroImg.getWidth(null)/2,
                e.getY()-hero.heroImg.getHeight(null)/2);
    }
}
